﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using WeatherReader;

public partial class EnergyEntry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {


            UpdateGas();
            UpdateElectricity();
        }
    }

    private void UpdateElectricity()
    {
        WeatherDbDataContext db = new WeatherDbDataContext();
        var electricity = db.DailyElectricities.OrderByDescending(de => de.de_dateTime);
        //dgElectricity.DataSource = electricity;

        var firstElectricity = electricity.FirstOrDefault();
        DateTime dtmElectricity;
        decimal electricityReading;
        if (firstElectricity != null)
        {
            dtmElectricity = firstElectricity.de_dateTime.AddDays(1);
            electricityReading = firstElectricity.de_kWhTotal.Value;

        }
        else
        {
            dtmElectricity = DateTime.Now.Date;
            electricityReading = 0;
        }
        txtElectricityDate.Text = dtmElectricity.ToString(CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat.ShortDatePattern);
        txtElectricityReading.Text = ((int)electricityReading).ToString();
    }
    protected void btnSubmitElectricity_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtElectricityDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal electricityReading = decimal.Parse(txtElectricityReading.Text);


       // var newReadings = UpdateHelper.CreateElectricityReadings(dtReadingDate, electricityReading);

        //lblElectricityInfo.Text = string.Format("Add {0} records, from {1} to {2}, average value {3}kWh?", newReadings.Count, newReadings.First().de_dateTime.ToLongDateString(), newReadings.Last().de_dateTime.ToLongDateString(), newReadings.First().de_kWhDaily);

        btnConfirmElectricity.Visible = true;
        btnCancelElectricity.Visible = true;
        btnSubmitElectricity.Visible = false;
       
    }
    protected void btnConfirmElectricity_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtElectricityDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal electricityReading = decimal.Parse(txtElectricityReading.Text);


        //var newReadings = UpdateHelper.CreateElectricityReadings(dtReadingDate, electricityReading);
        //UpdateHelper.SaveNewElectricityReadings(newReadings);

       // lblElectricityInfo.Text = string.Format("Added {0} records, from {1} to {2}, average value {3}kWh", newReadings.Count, newReadings.First().de_dateTime.ToLongDateString(), newReadings.Last().de_dateTime.ToLongDateString(), newReadings.First().de_kWhDaily);


        UpdateElectricity();
        btnConfirmElectricity.Visible = false;
        btnCancelElectricity.Visible = false;
        btnSubmitElectricity.Visible = true;
       }
    protected void btnCancelElectricity_Click(object sender, EventArgs e)
    {
        btnConfirmElectricity.Visible = false;
        btnCancelElectricity.Visible = false;
        btnSubmitElectricity.Visible = true;
    }
    protected void btnSubmitGas_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtGasDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal GasReading = decimal.Parse(txtGasReading.Text);


        //var newReadings = UpdateHelper.CreateGasReadings(dtReadingDate, GasReading);

        //lblGasInfo.Text = string.Format("Add {0} records, from {1} to {2}, average value {3}cubM?", newReadings.Count, newReadings.First().dg_dateTime.ToLongDateString(), newReadings.Last().dg_dateTime.ToLongDateString(), newReadings.First().dg_cubicMDaily);
        btnConfirmGas.Visible = true;
        btnCancelGas.Visible = true;
        btnSubmitGas.Visible = false;
    }
    protected void btnConfirmGas_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtGasDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal GasReading = decimal.Parse(txtGasReading.Text);


        //var newReadings = UpdateHelper.CreateGasReadings(dtReadingDate, GasReading);
        //UpdateHelper.SaveNewGasReadings(newReadings);

        //lblGasInfo.Text = string.Format("Added {0} records, from {1} to {2}, average value {3}cubM", newReadings.Count, newReadings.First().dg_dateTime.ToLongDateString(), newReadings.Last().dg_dateTime.ToLongDateString(), newReadings.First().dg_cubicMDaily);


        UpdateGas();
        btnConfirmGas.Visible = false;
        btnCancelGas.Visible = false;
        btnSubmitGas.Visible = true;
    }
    protected void btnCancelGas_Click(object sender, EventArgs e)
    {
        btnConfirmGas.Visible = false;
        btnCancelGas.Visible = false;
        btnSubmitGas.Visible = true;
    }

    private void UpdateGas()
    {
        WeatherDbDataContext db = new WeatherDbDataContext();
        var Gas = db.DailyGas.OrderByDescending(dg => dg.dg_dateTime);
        //dgGas.DataSource = Gas;

        var firstGas = Gas.FirstOrDefault();
        DateTime dtmGas;
        decimal GasReading;
        if (firstGas != null)
        {
            dtmGas = firstGas.dg_dateTime.AddDays(1);
            GasReading = firstGas.dg_cubicMTotal.Value;

        }
        else
        {
            dtmGas = DateTime.Now.Date;
            GasReading = 0;
        }
        txtGasDate.Text = dtmGas.ToString(CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat.ShortDatePattern);
        txtGasReading.Text = ((int)GasReading).ToString();
    }

}
